from django.db import models


# Create your models here.


# 多对一关系: (生产厂家(一) > 汽车(多))
class Manufacturer(models.Model):
    pass


class Car(models.Model):
    manufacturer = models.ForeignKey(Manufacturer, on_delete=models.CASCADE, verbose_name='生产厂家')


# 多对多关系: 如果 Pizza 含有多种 Topping``（配料） -- 也就是一种 ``Topping 可能存在于多个 Pizza 中，并且每个 Pizza  含有多种 Topping
class Topping(models.Model):
    pass


class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)


# 使用中间模型
class Member(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name


class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Member, through='Membership')

    def __str__(self):
        return self.name


class Membership(models.Model):
    member = models.ForeignKey(Member, on_delete=models.CASCADE)
    group = models.ForeignKey(Group, on_delete=models.CASCADE)
    data_join = models.DateField()
    invite_reason = models.CharField(max_length=64)


# 一对一关系
class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)

    def __str__(self):
        return '%s the place' % self.name


class Restaurant(models.Model):
    place = models.OneToOneField(Place, on_delete=models.CASCADE, primary_key=True)
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

    def __str__(self):
        return '%s the restaurant' % self.place.name


class Waiter(models.Model):
    restaurant = models.ForeignKey(Restaurant, on_delete=models.CASCADE)
    name = models.CharField(max_length=50)

    def __str__(self):
        return '%s the waiter at %s' % (self.name, self.restaurant)


# Meta 选项
class Ox(models.Model):
    horn_length = models.IntegerField()

    class Meta:
        ordering = ['horn_length']
        verbose_name_plural = 'oxen'


# 模型方法
class Person2(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    birth_date = models.DateField()

    def baby_boomer_status(self):
        """Returns the person`s baby-boomer status."""
        import datetime
        if self.birth_date < datetime.date(1945, 8, 1):
            return 'Pre-boomer'
        elif self.birth_date < datetime.date(1965, 1, 1):
            return 'Baby boomer'
        else:
            return 'Post-boomer'

    # 模型属性标签
    @property
    def full_name(self):
        """Returns the person`s full name."""
        return '%s %s' % (self.first_name, self.last_name)

    # 覆盖Override方法
    def save(self, *args, **kwargs):
        # do_something()
        super().save(*args, **kwargs)
        # do_something_else()

    def __str__(self):
        return self.first_name


# 抽象类模型
class CommonInfo(models.Model):
    name = models.CharField(max_length=100)
    age = models.PositiveIntegerField()

    class Meta:
        abstract = True


class Student(CommonInfo):
    home_group = models.CharField(max_length=5)


# 继承实现 OneToOneField
class Teacher(models.Model):
    name = models.CharField(max_length=20)
    sex = models.IntegerField()


class Student2(Teacher):
    age = models.IntegerField()
